var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var comment = require('../widget/comment/comment');
var storage = require('../../utils/storage');
var formatTime = require('../widget/time');
var filter = require('../widget/filter');
var commentList = require('../widget/commentList');
var goTop = require('../widget/goTop/goTop');
var timestamp = require('../widget/timestamp');


var shareContent = {};
var shareObj;
var pageSetting = {
    callback:function(componentName){
        var that = this;
        switch(componentName){
            case 'commentContent':
                var commentData = that.data['commentData'];
                commentFun(commentData,arguments[1])
                break;
            case 'likeFun':
                
                like(arguments[1]);
                break; 
            case 'delStairComment':
                
                delStairComment(arguments[1])
                break;
            case 'delcommentFun':
            
                delcommentFun(arguments[1])
                break;
            case 'removeBindtapFun':

                var removeFlag = arguments[1];
                removeBindtapFun(removeFlag);
                break;
            case 'delSecondComment':
                delSecondComment(arguments[1]);
                break;
            case 'pullUpLoad':
                pullUpLoad();
                break;
            case 'onCommentShow':
                removeBindtapFun({
                    'removebindtap':1
                });
                break;
            default:
                break;
        }

        //评论
        function commentFun(commentData,content){
            var data = {};
            
            if(commentData['type'] == 2){
                data = {
                    'self:community_api:community:comment':{
                        'articleId':commentData['articleid'],
                        'content':content,
                        'commentType':commentData['commenttype'],
                        'parentUserId':commentData['parentuserid'],
                        'parentCommentId':commentData['parentcommentid'],
                        'pParentCommentId':commentData['pparentcommentid'],
                        'relateInfo':commentData['relateinfo'],
                        'relatePPInfo':commentData['relateppinfo'],
                        'relatePPAccount':commentData['relateppaccount'],
                        'id':commentData['id'],
                        'come_from':commentData['comefrom'],
                        'relatePPAccountType':commentData['relateppaccounttype'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
            }else {
                
                data = {
                    'self:community_api:community:comment':{
                        'articleId':commentData['articleid'],
                        'content':content,
                        'commentType':commentData['commenttype'],
                        'parentUserId':commentData['parentuserid'],
                        'parentCommentId':commentData['parentcommentid'],
                        'pParentCommentId':commentData['pparentcommentid'],
                        'relateInfo':commentData['relateinfo'],
                        'relatePPInfo':commentData['relateppinfo'],
                        'relatePPAccount':commentData['relateppaccount'],
                        'id':commentData['id'],
                        'come_from':commentData['comefrom'],
                        'relatePPAccountType':commentData['relateppaccounttype'],
                        'relatePInfo':commentData['relatepinfo'],
                        'relatePAccount':commentData['relatepaccount'],
                        'relatePAccountType':commentData['relatepaccounttype'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
            }
        
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                var commentDetail = that['data']['commentDetail'];
                if( !!res['self:community_api:community:comment']['ret']){
                    // modal.toast(res['self:community_api:community:comment']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                } else {
                    if (commentData['type'] == 2){
                        var returnComment = res['self:community_api:community:comment']['data'];                             
                        var newComment = commentList.commentList(returnComment,commentData);

                        commentDetail['secondCommentInfo'].push(newComment);
                        commentDetail['commentInfo']['commentCount'] += 1;
                        
                        that.setData({
                            commentDetail:commentDetail
                        });
                    }else {
                        var returnComment = res['self:community_api:community:comment']['data'];
                        var newComment = commentList.commentList(returnComment,commentData); 
                        commentDetail['secondCommentInfo'].push(newComment);
                        commentDetail['commentInfo']['commentCount'] += 1; 
                        that.setData({
                            commentDetail:commentDetail
                        });
                    }    
                }
                
            })
        }

        //点赞功能
        function like(data){
            if(data['liketabflag'] != 0){
                return false;
            }
            that.setData({
                likeTabFlag:1
            });
            var likeData = {};
            if(data['type'] == -1){
                likeData = {
                    'self:community_api:community:like':{
                        'articleId':data['articleid'],
                        'id':data['id'],
                        'come_from':data['comefrom'],
                        'commentId':data['commentid'],
                        'likeType':data['type'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
            }else {
                likeData = {
                    'self:community_api:community:like':{
                        'articleId':data['articleid'],
                        'commentId':data['commentid'],
                        'likeType':data['type'],
                        'relateInfo':data['relateinfo'],
                        'relateAccount':data['relateaccount'],
                        'id':data['id'],
                        'come_from':data['comefrom'],
                        'relateAccountType':data['relateaccounttype'],
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
            }
            
            request.post({
                path: '/wxapp_api/api'
            },likeData)
            .then(function (res){

                if(res['self:community_api:community:like']['ret'] != 0){
                    // modal.toast(res['self:community_api:community:like']['msg']);
                    that.setData({
                        likeTabFlag:0
                    });
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                
                var commentDetail = that.data['commentDetail'];
                if(data.type != 1){
                    commentDetail['commentInfo']['myLike'] = 0;
                    commentDetail['commentInfo']['likeCount'] -= 1;
                }else{
                    commentDetail['commentInfo']['myLike'] = 1;
                    commentDetail['commentInfo']['likeCount'] += 1;
                }
                
                that.setData({
                    likeTabFlag:0,
                    commentDetail:commentDetail
                });
            })
        }

        //删除一级评论  ToDo
        function delStairComment(delData){
            // modal.loading();
            var data = {
                'self:community_api:community:deleteComment':{
                    'commentId':delData['commentid'],
                    'articleId':0,
                    'timestamp': timestamp['timestamp'],
                    'gtk': timestamp['gtk']
                }
            }
            
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if(res['self:community_api:community:deleteComment']['ret'] != 0){
                    // modal.toast(res['self:community_api:community:deleteComment']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                
                that.setData({
                    delFlag:false
                });
            })
        }

         // 删除按钮展示
        function delcommentFun(data){
            var commentDetail = that['data']['commentDetail'];
            
            for(var i = 0 ;i <commentDetail['secondCommentInfo'].length;i++){
                    var commentInfo = commentDetail['secondCommentInfo'][i]
                    commentInfo['isDel'] = true;
            }
            commentDetail['secondCommentInfo'][data.index]['isDel'] = false;
            that.setData({
                commentDetail:commentDetail,
                removeBindtap:1
            });
            
        }

        //删除按钮隐藏
        function removeBindtapFun(removeFlag){
            
            if(removeFlag.removebindtap == 1){
                var commentDetail = that['data']['commentDetail'];
                for(var i = 0 ;i <commentDetail['secondCommentInfo'].length;i++){
                    var commentInfo = commentDetail['secondCommentInfo'][i]
                    commentInfo['isDel'] = true;
                }
                that.setData({
                    commentDetail:commentDetail,
                    removeBindtap:0
                });
            }
        }

        //删除二级评论
        function delSecondComment(delSecondData){
            var data = {
                'self:community_api:community:deleteComment':{
                    'commentId':delSecondData['commentid'],
                    'articleId':0,
                    'timestamp': timestamp['timestamp'],
                    'gtk': timestamp['gtk']
                }
            }
            
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if(res['self:community_api:community:deleteComment']['ret'] != 0){
                    // modal.toast(res['self:community_api:community:like']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
                var commentDetail = that['data']['commentDetail'];
                commentDetail['secondCommentInfo'].splice([delSecondData.index],1);
                commentDetail['commentInfo']['commentCount'] -= 1;
                that.setData({
                    commentDetail:commentDetail
                });
            })
         }


         //上拉加载
         function pullUpLoad(){
            that.setData({
                upLoadFlag:0
            });
            modal.loading();

            var data = {
                'self:community_api:community:secondCommentList':{
                    'articleId':that['data']['commentDetailIds']['articleId'],
                    'act_id':that.data.topId,
                    'commentId':that['data']['commentDetailIds']['commentId'],
                    'page':that['data']['upLoadPage'],
                    'pageSize':20,
                    'withInfo':0
                }
            };

            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if(res['self:community_api:community:secondCommentList']['ret'] != 0 ){
                // modal.toast(res['self:community_api:community:secondCommentList']['msg']);
                    // that.setData({
                    //     delFlag:false
                    // })
                    modal.hide();
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }

                var commentDetailData = res['self:community_api:community:secondCommentList']['data'];
                
                if(commentDetailData['hasNext'] != 1){
                    that.setData({
                        upLoadFlag:0
                    });
                }else{
                    that.setData({
                        upLoadFlag:1
                    });
                }

                for (var i = 0; i<commentDetailData['secondCommentInfo'].length;i++){
                    var commentDetailDataItem = commentDetailData['secondCommentInfo'][i];
                    commentDetailData['secondCommentInfo'][i]['content'] = filter.filter(commentDetailDataItem['content']);
                    commentDetailData['secondCommentInfo'][i]['isDel'] = true;
                }

                that['data']['commentDetail']['secondCommentInfo'] = that['data']['commentDetail']['secondCommentInfo'].concat(commentDetailData['secondCommentInfo']);


                that.setData({
                    commentDetail:that['data']['commentDetail']
                });
                 modal.hide();
            });
         }
    },
    data: {
        delFlag:true,
        comeFrom: '',
        topId: '',
        actName: '',
        nick: '',
        url: '',
        medalMaskFlag:false
    },
    onLoad: function(options) {
        console.log(options)
        modal.loading();
        var that = this;
        var comeFrom = that.data['comeFrom'];
        var topId = that.data['topId'];
        var actName = that.data['actName'];
        var nick = that.data['nick'];
        var url = that.data['url'];
        app.getUserInfo(function (userInfo) {
          that.setData({
            nick: userInfo.nickName
          });
        });
        that.setData({
            commentDetailIds:{
                'articleId':options.articleId,
                'commentId':options.commentId,
                'id':options['id']
            },
            comeFrom: options['comeFrom'],
            topId: options['id'],
            actName : options['actName']
        })
        var data = {
            'self:community_api:community:secondCommentList':{
                'articleId':options.articleId,
                'act_id':options.act_id,
                'commentId':options.commentId,
                'page':1,
                'pageSize':20,
                'withInfo':1
            }
        }  
        
        request.post({
            path: '/wxapp_api/api'
        },data)
        .then(function (res){
            if(res['self:community_api:community:secondCommentList']['ret'] != 0 ){
                // modal.toast(res['self:community_api:community:secondCommentList']['msg']);
                // that.setData({
                //     delFlag:false
                // })
                modal.hide();
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            } else {
                var commentDetailData = res['self:community_api:community:secondCommentList']['data'];
                if(commentDetailData == ''){
                    modal.hide();
                    modal.toast('该条评论的主人狠心删掉了它','提示',3000,function(){
                        setTimeout(function(){
                            wx.navigateBack({
                                delta: 1
                            })
                        },3000);  
                    });
                    return false;
                }
                if(commentDetailData['hasNext'] != 1){
                    that.setData({
                        upLoadFlag:0
                    });
                }

                commentDetailData['commentInfo']['id'] = options['id'];
                commentDetailData['commentInfo']['userInfo']['face'] = filter.filter(commentDetailData['commentInfo']['userInfo']['face']);
                commentDetailData['commentInfo']['createTime'] = formatTime.formatTime(commentDetailData['commentInfo']['createTime'])
                for (var i = 0; i<commentDetailData['secondCommentInfo'].length;i++){
                    var commentDetailDataItem = commentDetailData['secondCommentInfo'][i];
                    commentDetailData['secondCommentInfo'][i]['content'] = filter.filter(commentDetailDataItem['content']);
                    commentDetailData['secondCommentInfo'][i]['isDel'] = true;
                }
                // nick = commentDetailData.commentInfo.userInfo.nick;
                if(options.comeFrom == 'dp_order'){
                    url = "articleId="+options.articleId+"&id="+options.id+"&commentId="+options.commentId+"&act_id="+options.act_id+"&comeFrom="+options.comeFrom+"&actName="+options.actName;
                    console.log('/pages/valuation-detail/valuation-detail?'+url)
                    that.setData({
                        url: url
                    })
                }
                that.setData({
                    commentDetail: commentDetailData
                });
                modal.hide();    
            }
        });
    },
    onShareAppMessage: function (res) {
        return {
            title: this.data.nick+'觉得这条企鹅优品-'+this.data.actName+'的售后评价很赞哟，快来围观吧！',
            path: '/pages/valuation-detail/valuation-detail?'+this.data.url,
            success: function(res) {
            // 转发成功
            },
            fail: function(res) {
            // 转发失败
            }
        }   
    },
    showMedalMask: function(){
      this.setData({
          medalMaskFlag : true
      });
    },
    hideMedalMask: function(){
      this.setData({
          medalMaskFlag : false
      });
  },
};

utils.assign(pageSetting,comment,goTop);
Page(pageSetting);